<template>
  <div style="width: 100%;height: 100vh;background-color: chocolate;overflow: hidden">
    <div style="width: 400px ; margin: 150px auto">
      <div style="color: #cccccc; font-size: 24px;text-align: center;padding: 30px">欢迎注册</div>
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="用户名：" prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱号：" prop="email">
          <el-input prefix-icon="el-icon-user-solid" v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input  v-model="form.password" show-password></el-input>
          <!--        prefix-icon="el-icon-lock"-->
        </el-form-item>
        <el-form-item label="确认密码：" prop="confirm">
          <el-input  v-model="form.confirm" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 50%" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Register",
  data()
  {
    return {
      form: {},
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        email:[
          {
            required: true,
            message: '请输入邮箱号',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
        confirm: [
          {
            required: true,
            message: '请再次输入密码',
            trigger: 'blur',
          },
        ],
      }
    }
  },
  methods: {
    register(){
      if(this.form.password != this.form.confirm){
        this.$message(
            {
              type:"error",
              message:'两次输入密码不同'
            }
        )
        return
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/register",this.form).then(res =>{
            if (res.code === '0') {
              this.$message(
                  {
                    type:"success",
                    message:"注册成功"
                  }
              )
              this.$router.push("/login")      //跳转到登陆页面  index.js里面有路由

            } else {
              this.$message(
                  {
                    type:"error",
                    message:res.msg
                  }
              )
            }
          })
        }
      })
    }
  }
}

</script>

<style scoped>

</style>
//前端写好后再写接口